<?php
class Model_ServiceClicksor extends Model_Service
{
    public function loginCheck ($dpt_id, $dpt_account_login, $password = "")
    {
        $service_info = $this->getServiceInfo($dpt_id);
        $dbconn = $this->getServiceConn($dpt_id);
        //Zend_Debug::dump($dbconn);
        //Zend_Debug::dump($service_info);
        if (isset($dbconn['admin'])) {
            $account_info = explode('-', $dpt_account_login); // 'networkid-A-accountid'
            if (count($account_info) != 1 &&
             count($account_info) != 3) {
                $rs['status'] = false;
                $rs['message'] = 'account format error';
            } else {
                if (count($account_info) == 1)
                    $account_type = 'ADMIN';
                else {
                    $account_type = strtoupper($account_info[1]);
                    $account_id = $account_info[2];
                }
                if ($account_type == 'A') {
                    $select = $dbconn['admin']->select()->from('advertisers');
                    $select->where('adsid = ?', (int) $account_id);
                } else 
                    if ($account_type == 'P') {
                        $select = $dbconn['admin']->select()->from('users');
                        $select->where('userID = ?', (int) $account_id);
                    } else {
                        $select = $dbconn['admin']->select()->from('admins');
                        $select->where('aid = ?', $dpt_account_login);
                    }
                $user = $dbconn['admin']->fetchRow($select);
                //Zend_Debug::dump($user);//exit;
                $passwd = md5($password);
                if ($account_type == 'A')
                    $passwd = $password;
                elseif ($account_type == 'P')
                    $passwd = sha1($password);
                else
                    $passwd = $password;
                $status = $user['status'];
                $passwd_stored = $user['password'];
                if (! $user) {
                    $rs['status'] = false;
                    $rs['message'] = 'account not found';
                } elseif (($status == 'Suspend') || ($status == 'Disable') ||
                 ($status == 'Cancel')) {
                    $rs['status'] = false;
                    $rs['message'] = 'account not active';
                } elseif ($password && $passwd != $passwd_stored) {
                    $rs['status'] = false;
                    $rs['message'] = 'wrong password';
                } else {
                    $rs['status'] = true;
                    if ($account_type == 'A') {
                        $rs['dpt_account_id'] = $user['adsid'];
                        $rs['login_area'] = 'advertiser';
                    } else 
                        if ($account_type == 'P') {
                            $rs['dpt_account_id'] = $user['userID'];
                            $rs['login_area'] = 'publisher';
                        } else {
                            $rs['dpt_account_id'] = $user['admin_id'];
                            $rs['login_area'] = 'admin';
                        }
                    $rs['email'] = $user['email'];
                    $rs['dpt_account_login'] = $dpt_account_login;
                }
            }
        } else {
            $rs['status'] = false;
            $rs['message'] = 'service connection error';
        }
        return $rs; // return OK if passed
    }
    
    
	public function createAccount($user_id, $dpt_id, $profile)
	{
		$password = Model_User::str_makerand(6,8); //create a random password for remote account
		
		$params=array();
		$params['password']=$password;

		if (is_array($profile)){
			$name = $profile['first_name'].' '.$profile['last_name'];
			
			$params['name']=$name;
			foreach($profile as $key => $value){
				switch($key){
					case 'email':
					case 'company':
					case 'phone':
					case 'address':
					case 'city':
					case 'state':
					case 'zipcode':
					case 'country':
					case 'timezone_id':
					case 'website':
						if (!empty($value)) $params[ $key ] = $value;
						break;
					default: break;
				}
			}
			
		}
		
		$params['network_id']='1';  //for clicksor.com
		
		$area = isset($profile['area']) ? $profile['area'] : 'advertiser';  //for account type:advertiser,publisher,admin
		$params['area']=$area;
		
		$remote_call = 'http://admin.clicksor.com/admin/server-rpc.php';
		$client = new Zend_XmlRpc_Client($remote_call);		
		$account_id = $client->call("YesupAcc.addUser",array($params));
		
		if ($account_id>0){
			if ($area=='publisher')
				$login_account = '100-P-'.$account_id;
			else 
				$login_account = '100-A-'.$account_id;
			
			$dpt_login_arr=array();
			$dpt_login_arr['dpt_account_login'] = $login_account;
			$dpt_login_arr['login_area'] = $area;
			$dpt_login_arr['email'] = $profile['email'];
			$dpt_login_arr['password'] = $password;
			$dpt_login_arr['admin_user_id'] = '';
			$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
			$dpt_account_id = $account_id;
			
			$model_client = new Model_UserClient();
			$result = $model_client->linkClientService($user_id, $dpt_id, $dpt_account_id, $dpt_login_arr);
			if ($result){
				$ss = new Zend_Session_Namespace('service');
				$ss->link_result = 'success';
				$ss->link_dpt_id = $dpt_id;
				$ss->link_login_account = $login_account;
				$ss->link_dpt_account_id = $account_id;
				
				return true;
			}else 
				return false;			
		}else{
			return false;
		}
		
	}
    
    
}